#include "MyNc4Parse.h"
#include "time.h"

MyNc4Parse::MyNc4Parse(const char* file_path)
{
    this->nc_dir = file_path;
}

auto MyNc4Parse::inst3_3d_aer_single3d(const char* nc_name, const char* values, Coord3Dt &coordinate) -> int
{
    char* nc_path = new char[50];

    strcpy(nc_path, this->nc_dir);
    strcat(nc_path, nc_name);
    NcFile nc(nc_path, NcFile::ReadOnly);

    clock_t start1 = clock();   //结束时间
    if (!nc.is_valid()) {
        std::cerr << "unable to open file" << std::endl;
        return -1;
    }
    clock_t end1 = clock();   //结束时间

    clock_t start2 = clock();
    NcVar* lwt = nc.get_var(values);
    NcVar* tim = nc.get_var("time"); 
    NcVar* lat = nc.get_var("lat"); 
    NcVar* lon = nc.get_var("lon"); 
    clock_t end2 = clock();   //结束时间

    int dim_time = lwt->get_dim(0)->size(); // 获取第一个维度的长度
    int dim_lat = lwt->get_dim(1)->size(); 
    int dim_lon = lwt->get_dim(2)->size();

    //int* timeIn = (int*)malloc(8 * sizeof(int));
    //float* latIn = (float*)malloc(361 * sizeof(float));
    //float* lonIn = (float*)malloc(576 * sizeof(float));
    int timeIn[8];
    float latIn[361];
    float lonIn[576];
    float(*prect)[361][576] = (float(*)[361][576])malloc(dim_time * sizeof(float[361][576]));
    
    clock_t start = clock();
    try
    {
        tim->get(&timeIn[0], 8);
        lat->get(&latIn[0], 361);
        lon->get(&lonIn[0], 576);
        lwt->get(&prect[0][0][0], dim_time, dim_lat, dim_lon); // 将数据读入数组   
    }
    catch (const std::exception&)
    {
        std::cerr << "get values to array fail" << std::endl;

    }
    clock_t end = clock();   //结束时间

    int timei = this->sequential_select(timeIn, dim_time, coordinate.time, timei);
    if (timei < 0) {
        std::cerr << "find timei fail" << std::endl;
        return -1;
    }
    int lati = this->binary_select(latIn, dim_lat, coordinate.lat, lati);
    if (lati < 0) {
        std::cerr << "find lat fail" << std::endl;
        return -1;
    }
    int loni = this->binary_select(lonIn, dim_lon, coordinate.lon, loni);
    if (lati < 0) {
        std::cerr << "find lon fail" << std::endl;
        return -1;
    }
    coordinate.values = prect[timei][lati][loni];
    std::cout << "the 3d value you want find: "\
     <<" time: "<< coordinate .time <<" lat: "<< coordinate .lat<<" lon: "<< coordinate.lon \
        <<" = " << prect[timei][lati][loni] << '\n';
    std::cout << "time1 = " << double(end1 - start1) / CLOCKS_PER_SEC << "s" << std::endl;  //输出时间（单位：ｓ）
    std::cout << "time2 = " << double(end2 - start2) / CLOCKS_PER_SEC << "s" << std::endl;  //输出时间（单位：ｓ）
    std::cout << "time3 = " << double(end - start) / CLOCKS_PER_SEC << "s" << std::endl;  //输出时间（单位：ｓ）

    free(prect);
    delete[] nc_path;
    return 0;
}

auto MyNc4Parse::inst3_3d_aer_single_multi3d(const char* nc_name, const char* values, std::vector<Coord3Dt> &coordsets) -> int
{
    char* nc_path = new char[50];

    strcpy(nc_path, this->nc_dir);
    strcat(nc_path, nc_name);
    NcFile nc(nc_path, NcFile::ReadOnly);

    clock_t start1 = clock();   //结束时间
    if (!nc.is_valid()) {
        std::cerr << "unable to open file" << std::endl;
        return -1;
    }
    clock_t end1 = clock();   //结束时间

    clock_t start2 = clock();
    NcVar* lwt = nc.get_var(values);
    NcVar* tim = nc.get_var("time");
    NcVar* lat = nc.get_var("lat");
    NcVar* lon = nc.get_var("lon");
    clock_t end2 = clock();   //结束时间

    int dim_time = lwt->get_dim(0)->size(); // 获取第一个维度的长度
    int dim_lat = lwt->get_dim(1)->size();
    int dim_lon = lwt->get_dim(2)->size();

    //int* timeIn = (int*)malloc(8 * sizeof(int));
    //float* latIn = (float*)malloc(361 * sizeof(float));
    //float* lonIn = (float*)malloc(576 * sizeof(float));
    int timeIn[8];
    float latIn[361];
    float lonIn[576];
    float(*prect)[361][576] = (float(*)[361][576])malloc(dim_time * sizeof(float[361][576]));

    clock_t start = clock();
    try
    {
        tim->get(&timeIn[0], 8);
        lat->get(&latIn[0], 361);
        lon->get(&lonIn[0], 576);
        lwt->get(&prect[0][0][0], dim_time, dim_lat, dim_lon); // 将数据读入数组   
    }
    catch (const std::exception&)
    {
        std::cerr << "get values to array fail" << std::endl;

    }
    clock_t end = clock();   //结束时间


    for (int i = 0; i < coordsets.size(); i++) {
        int timei = this->sequential_select(timeIn, dim_time, coordsets[i].time, timei);
        if (timei < 0) {
            std::cerr << "find timei fail" << std::endl;
            return -1;
        }
        int lati = this->binary_select(latIn, dim_lat, coordsets[i].lat, lati);
        if (lati < 0) {
            std::cerr << "find lat fail" << std::endl;
            return -1;
        }
        int loni = this->binary_select(lonIn, dim_lon, coordsets[i].lon, loni);
        if (lati < 0) {
            std::cerr << "find lon fail" << std::endl;
            return -1;
        }
        coordsets[i].values = prect[timei][lati][loni];
    }

    free(prect);
    delete[] nc_path;
    return 0;
}

auto MyNc4Parse::inst3_3d_aer_single4d(const char* nc_name, const char* values, Coord4Dt& coordinate) -> int
{
    char* nc_path = new char[50];

    strcpy(nc_path, this->nc_dir);
    strcat(nc_path, nc_name);
    NcFile nc(nc_path, NcFile::ReadOnly);

    clock_t start1 = clock();   //结束时间
    if (!nc.is_valid()) {
        std::cerr << "unable to open file" << std::endl;
        return -1;
    }
    clock_t end1 = clock();   //结束时间

    clock_t start2 = clock();
    NcVar* lwt = nc.get_var(values);
    NcVar* tim = nc.get_var("time");
    NcVar* lev = nc.get_var("lev");
    NcVar* lat = nc.get_var("lat");
    NcVar* lon = nc.get_var("lon");
    clock_t end2 = clock();   //结束时间

    int dim_time = lwt->get_dim(0)->size(); // 获取第一个维度的长度
    int dim_lev = lwt->get_dim(1)->size();
    int dim_lat = lwt->get_dim(2)->size();
    int dim_lon = lwt->get_dim(3)->size();

    //int* timeIn = (int*)malloc(8 * sizeof(int));
    //float* latIn = (float*)malloc(361 * sizeof(float));
    //float* lonIn = (float*)malloc(576 * sizeof(float));
    int timeIn[8];
    int levIn[72];
    float latIn[361];
    float lonIn[576];
    //float(*prect)[361][576] = (float(*)[361][576])malloc(dim_time * sizeof(float[361][576]));

    //float**** prect;
    int i, j, k;

    //// 为第一维分配内存
    float**** prect = (float****)malloc(8 * sizeof(float***));

    // 为每个元素分配内存
    for (i = 0; i < 8; i++) {
        prect[i] = (float***)malloc(72 * sizeof(float**));
        for (j = 0; j < 72; j++) {
            prect[i][j] = (float**)malloc(361 * sizeof(float*));
            for (k = 0; k < 361; k++) {
                prect[i][j][k] = (float*)malloc(576 * sizeof(float));
            }
        }
    }

    clock_t start = clock();
    try
    {
        tim->get(&timeIn[0], 8);
        lat->get(&latIn[0], 361);
        lon->get(&lonIn[0], 576);
        lwt->get(&prect[0][0][0][0], 8, dim_lev,dim_lat, dim_lon); // 将数据读入数组   
    }
    catch (const std::exception&)
    {
        std::cerr << "get values to array fail" << std::endl;

    }
    clock_t end = clock();   //结束时间

    int timei = this->sequential_select(timeIn, dim_time, coordinate.time, timei);
    if (timei < 0) {
        std::cerr << "find timei fail" << std::endl;
        return -1;
    }
    int lati = this->binary_select(latIn, dim_lat, coordinate.lat, lati);
    if (lati < 0) {
        std::cerr << "find lat fail" << std::endl;
        return -1;
    }
    int loni = this->binary_select(lonIn, dim_lon, coordinate.lon, loni);
    if (lati < 0) {
        std::cerr << "find lon fail" << std::endl;
        return -1;
    }
    //coordinate.values = prect[timei][lati][loni];
    //std::cout << "the 3d value you want find: "\
    //    << " time: " << coordinate.time << " lat: " << coordinate.lat << " lon: " << coordinate.lon \
    //    << " = " << prect[timei][lati][loni] << '\n';
    std::cout << "time1 = " << double(end1 - start1) / CLOCKS_PER_SEC << "s" << std::endl;  //输出时间（单位：ｓ）
    std::cout << "time2 = " << double(end2 - start2) / CLOCKS_PER_SEC << "s" << std::endl;  //输出时间（单位：ｓ）
    std::cout << "time3 = " << double(end - start) / CLOCKS_PER_SEC << "s" << std::endl;  //输出时间（单位：ｓ）

    free(prect);
    delete[] nc_path;
    return 0;
}

auto MyNc4Parse::inst3_3d_aer_single4d_useC() -> int
{
    int ncid, pres_varid, temp_varid, msa_varid;
    int lat_varid, lon_varid;

    /* The start and count arrays will tell the netCDF library where to
       read our data. */
    size_t start[NDIMS], count[NDIMS];

    /* Program variables to hold the data we will read. We will only
       need enough space to hold one timestep of data; one record. */
    //float msa_in[NLVL][NLAT][NLON];
    //float temp_in[NLVL][NLAT][NLON];
    double(*msa_in)[361][576] = (double(*)[361][576])malloc(72 * sizeof(double[361][576]));
    //int(*msa_in)[361][576] = (int(*)[361][576])malloc(72 * sizeof(int[361][576]));
    /* These program variables hold the latitudes and longitudes. */
    float lats[NLAT], lons[NLON];

    /* Loop indexes. */
    int lvl, lat, lon, rec, i = 0;

    /* Error handling. */
    int retval;

    /* Open the file. */
    if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
        ERR(retval);

    /* Get the varids of the latitude and longitude coordinate
     * variables. */
    if ((retval = nc_inq_varid(ncid, LAT_NAME, &lat_varid)))
        ERR(retval);
    if ((retval = nc_inq_varid(ncid, LON_NAME, &lon_varid)))
        ERR(retval);

    /* Read the coordinate variable data. */
    if ((retval = nc_get_var_float(ncid, lat_varid, &lats[0])))
        ERR(retval);
    if ((retval = nc_get_var_float(ncid, lon_varid, &lons[0])))
        ERR(retval);

    /* Check the coordinate variable data. */
    //for (lat = 0; lat < NLAT; lat++)
    //    if (lats[lat] != START_LAT + 5. * lat)
    //        return 2;
    //for (lon = 0; lon < NLON; lon++)
    //    if (lons[lon] != START_LON + 5. * lon)
    //        return 2;

    /* Get the varids of the pressure and temperature netCDF
     * variables. */
    if ((retval = nc_inq_varid(ncid, MSA_NAME, &msa_varid)))
        ERR(retval);


    /* Read the data. Since we know the contents of the file we know
     * that the data arrays in this program are the correct size to
     * hold one timestep. */
    count[0] = 1;
    count[1] = NLVL;
    count[2] = NLAT;
    count[3] = NLON;
    start[1] = 0;
    start[2] = 0;
    start[3] = 0;

    std::vector<double(*)[361][576]> vectorOfPointers;
    /* Read and check one record at a time. */
    for (rec = 0; rec < NREC; rec++)
    {
        start[0] = rec;
        if ((retval = nc_get_vara_double(ncid, msa_varid, start,
            count, &msa_in[0][0][0])))
            ERR(retval);
        vectorOfPointers.push_back(msa_in);
        //if ((retval = nc_get_vara_float(ncid, temp_varid, start,
        //    count, &temp_in[0][0][0])))
        //    ERR(retval);

        /* Check the data. */
        i = 0;
        //for (lvl = 0; lvl < NLVL; lvl++)
        //    for (lat = 0; lat < NLAT; lat++)
        //        for (lon = 0; lon < NLON; lon++)
        //        {
        //            if (msa_in[lvl][lat][lon] != SAMPLE_PRESSURE + i)
        //                return 2;
        //            //if (temp_in[lvl][lat][lon] != SAMPLE_TEMP + i)
        //            //    return 2;
        //            i++;
        //        }

    } /* next record */

    /* Close the file. */
    if ((retval = nc_close(ncid)))
        ERR(retval);

    printf("*** SUCCESS reading example file pres_temp_4D.nc!\n");
    return 0;
}

auto MyNc4Parse::stat2_2d_pct(const char* nc_name) -> int
{
    char* nc_path = new char[50];
    strcpy(nc_path, this->nc_dir);
    strcat(nc_path, nc_name);
    char* latsIn = new char[100];
    char* lonsIn = new char[100];

    NcFile nc(nc_path, NcFile::ReadOnly);

    if (!nc.is_valid()) {
        std::cerr << "Unable to open file" << std::endl;
        return -1;
    }

    //basemes_of_values *pct2d = new struct basemes_of_values;
#if 0
    try
    {
        // 获取维度信息
        NcDim* LAT = nc.get_dim("lat");
        NcDim* LON = nc.get_dim("lon");
        NcDim* PRECTOT = nc.get_dim("PRECTOT");
        NcDim* T2MMAX = nc.get_dim("T2MMAX");
        NcDim* T2MMEAN = nc.get_dim("T2MMEAN");
        NcDim* T2MMIN = nc.get_dim("T2MMIN");
        NcDim* TIME = nc.get_dim("time");

        int num_LAT = LAT->size();
        int num_num_LON = LON->size();
        int num_PRECTOT= PRECTOT->size();
        int num_T2MMAX = T2MMAX->size();
        int num_T2MMEAN= T2MMEAN->size();
        int num_T2MMIN = T2MMIN->size();
        int num_TIME = TIME->size();
    }
    catch (const std::exception&)
    {
        std::cerr << "fail to get dim,through" << '\n';
    }

    try
    {
        // 获取尺寸信息
        int num_LAT = nc.get_dim("lat")->size();
        int num_LON = nc.get_dim("lon")->size();
        int num_PRECTOT = nc.get_dim("PRECTOT")->size();
        int num_T2MMAX = nc.get_dim("T2MMAX")->size();
        int num_T2MMEAN = nc.get_dim("T2MMEAN")->size();
        int num_T2MMIN = nc.get_dim("T2MMIN")->size();
        int num_TIME = nc.get_dim("time")->size();
    }
    catch (const std::exception&)
    {
        std::cerr << "fail to get size,through" << '\n';
    }
#endif

    NcVar* pre = nc.get_var("PRECTOT"); // 获取变量
    NcVar* lat = nc.get_var("lat"); // 获取变量
    NcVar* lon = nc.get_var("lon"); // 获取变量

    int dim_time = pre->get_dim(0)->size(); // 获取第一个维度的长度
    int dim_lat = pre->get_dim(1)->size(); // 获取第一个维度的长度
    int dim_lon = pre->get_dim(2)->size(); // 获取第一个维度的长度

#if 0
    // 动态分配内存
    float*** prect = new float** [dim_time];
    for (int i = 0; i < dim_time; i++) {
        prect[i] = new float* [dim_lat];
        for (int j = 0; j < dim_lat; j++) {
            prect[i][j] = new float[dim_lon];
        }
    }

    // 初始化数组
    for (int i = 0; i < dim_time; i++) {
        for (int j = 0; j < dim_lat; j++) {
            for (int k = 0; k < dim_lon; k++) {
                prect[i][j][k] = i + j + k;
            }
        }
    }
    //float prect[1][361][576];
     // 访问和打印数组元素

    //for (int i = 0; i < dim_time; i++) {
    //    for (int j = 0; j < dim_lat; j++) {
    //        for (int k = 0; k < dim_lon; k++) {
    //            std::cout << prect[i][j][k] << " ";
    //        }
    //        std::cout << std::endl;
    //    }
    //    std::cout << std::endl;
    //}

    // 释放内存
    //for (int i = 0; i < dim_time; i++) {
    //    for (int j = 0; j < dim_lat; j++) {
    //        delete[] prect[i][j];
    //    }
    //    delete[] prect[i];
    //}
    //delete[] prect;

#endif
    float latiIn[361];
    float lonIn[576];
    float prect[1][361][576];

    lat->get(latiIn,dim_lat); // 将数据读入数组
    lon->get(lonIn,dim_lon); // 将数据读入数组
    pre->get(&prect[0][0][0], dim_time, dim_lat, dim_lon); // 将数据读入数组
    
    //查询一个坐标
    float latitude = -81.0, longitude = -175.0;

#if 0
    for (int la = 0; la < dim_lat; la++) {
        if (latiIn[la] == latitude) {
            for (int lo = 0; lo < dim_lon; lo++) {
                if (lonIn[lo] == longitude) {
                    std::cout << prect[0][la][lo] << std::endl;
                    return 0;
                }
            }
        }
    }
#endif
    //折半查询
    int lati = -1, loni = -1;
    lati = this->binary_select(latiIn,361, latitude,lati);
    if (lati < 0) {
        std::cerr << "find lat fail" << std::endl;
        return -1;
    }
    lati = this->binary_select(lonIn, 576, longitude,loni);
    if (loni < 0) {
        std::cerr << "find lon fail" << std::endl;
        return -1;
    }
    std::cout << prect[0][lati][loni] << std::endl;
    delete[]nc_path;
    delete[]latsIn;
    delete[]lonIn;

    return 0;
}

auto MyNc4Parse::stat2_2d_pct_single(const char* nc_name,const char* values, Coord2Dt &coordinate) -> int
{

    clock_t start = clock();
    char* nc_path = new char[50];
    strcpy(nc_path, this->nc_dir);
    strcat(nc_path, nc_name);
    char* latsIn = new char[100];
    char* lonsIn = new char[100];

    NcFile nc(nc_path, NcFile::ReadOnly);

    if (!nc.is_valid()) {
        std::cerr << "Unable to open file" << std::endl;
        return -1;
    }

    NcVar* pre = nc.get_var(values); // 获取变量
    NcVar* lat = nc.get_var("lat"); // 获取变量
    NcVar* lon = nc.get_var("lon"); // 获取变量

    int dim_time = pre->get_dim(0)->size(); // 获取第一个维度的长度
    int dim_lat = pre->get_dim(1)->size(); // 获取第一个维度的长度
    int dim_lon = pre->get_dim(2)->size(); // 获取第一个维度的长度

    float latiIn[361];
    float lonIn[576];
    float prect[1][361][576];

    lat->get(latiIn, dim_lat); // 将数据读入数组
    lon->get(lonIn, dim_lon); // 将数据读入数组
    pre->get(&prect[0][0][0], dim_time, dim_lat, dim_lon); // 将数据读入数组

    //查询一个坐标
    //float latitude = -81.0, longitude = -175.0;
    

    //折半查询
    int lati = -1, loni = -1;
    lati = this->binary_select(latiIn, 361, coordinate.lat, lati);
    if (lati < 0) {
        std::cerr << "find lat fail" << std::endl;
        return -1;
    }
    lati = this->binary_select(lonIn, 576, coordinate.lon, loni);
    if (loni < 0) {
        std::cerr << "find lon fail" << std::endl;
        return -1;
    }


    std::cout << prect[0][lati][loni] << std::endl;
    coordinate.value = prect[0][lati][loni];
    delete[]nc_path;
    delete[]latsIn;
    delete[]lonIn;

    clock_t end = clock();   //结束时间
    std::cout << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << std::endl;  //输出时间（单位：ｓ）
    
    
    return 0;
}

auto MyNc4Parse::stat2_2d_pct_multi(const char* nc_name, const char* values, std::vector<Coord2Dt> &coordsets) -> int
{
    char* nc_path = new char[100];
    strcpy(nc_path, nc_name);
    char* latsIn = new char[361];
    char* lonsIn = new char[576];


    NcFile nc(nc_path, NcFile::ReadOnly);

    if (!nc.is_valid()) {
        std::cerr << "Unable to open file" << std::endl;
        return -1;
    }

    NcVar* pre = nc.get_var(values); // 获取变量
    NcVar* lat = nc.get_var("lat"); // 获取变量
    NcVar* lon = nc.get_var("lon"); // 获取变量

    int dim_time = pre->get_dim(0)->size(); // 获取第一个维度的长度
    int dim_lat = pre->get_dim(1)->size(); // 获取第一个维度的长度
    int dim_lon = pre->get_dim(2)->size(); // 获取第一个维度的长度

    float latiIn[361];
    float lonIn[576];
    float prect[1][361][576];

    lat->get(latiIn, dim_lat); // 将数据读入数组
    lon->get(lonIn, dim_lon); // 将数据读入数组
    pre->get(&prect[0][0][0], dim_time, dim_lat, dim_lon); // 将数据读入数组

    //查询一个坐标
    //float latitude = -81.0, longitude = -175.0;
    omp_set_num_threads(nThreadsNum);
    double start_time, run_time;
    start_time = omp_get_wtime();

#if 0
#pragma omp parallel
    {
    #pragma omp for
        for (int i = 0; i < coordsets.size(); i++) {
            //折半查询
            int lati = -1, loni = -1;
            lati = this->binary_select(latiIn, 361, coordsets[i].lat, lati);
            if (lati < 0) {
                std::cerr << "find lat fail" << std::endl;
                return -1;
            }
            lati = this->binary_select(lonIn, 576, coordsets[i].lon, loni);
            if (loni < 0) {
                std::cerr << "find lon fail" << std::endl;
                return -1;
            }
            //std::cout << prect[0][lati][loni] << "\n";
        }
    }
    run_time = omp_get_wtime() - start_time;
    printf("run_time: %.6lf\n", run_time);
#endif    

    clock_t start = clock();
    for (int i = 0; i < coordsets.size();i++) {
        //折半查询
        int lati = -1, loni = -1;
        lati = this->binary_select(latiIn, 361, coordsets[i].lat, lati);
        if (lati < 0) {
            std::cerr << "find lat: "<< coordsets[i].lat <<" fail" << std::endl;
            return -1;
        }
        loni = this->binary_select(lonIn, 576, coordsets[i].lon, loni);
        if (loni < 0) {
            std::cerr << "find lon："<< coordsets[i].lon <<" fail" << std::endl;
            return -1;
        }
        this->mtx.lock();
        //std::this_thread::sleep_for(std::chrono::seconds(1)); 
        coordsets[i].value = prect[0][lati][loni];
        std::cout << values <<" : " << prect[0][lati][loni] << "\n";
        this->mtx.unlock();
    }
    clock_t end = clock();   //结束时间
    std::cout << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << std::endl;  //输出时间（单位：ｓ）

    delete[] nc_path;
    delete[] latsIn;
    delete[] lonsIn;

    return 0;
}

int MyNc4Parse::ParseM2tmnx(std::vector<std::string> values, std::vector<Coord2Dt>& coordsets)
{
    std::vector<std::string> files;
    getAllFiles(this->nc_dir, files, ".nc4");
    for (auto it : files) {
        //coordsets.clear();
        for (auto it2: values) {
            //stat2_2d_pct_single(it.c_str(), it2.c_str(), coordinate); 
            stat2_2d_pct_multi(it.c_str(), it2.c_str(), coordsets);
        }
    }

    return 0;
}

auto MyNc4Parse::sequential_select(int a[], int n, int key, int& no) -> int
{   
    bool flag = false;
    for (int i = 0; i < n && flag == false; i++) {
        if (a[i] == key) {
            flag = true;
            no = i;
        }
    }
    if (flag == true)
        return no;
    return -1;

}

auto MyNc4Parse::binary_select(float a[],int n,float key,int& no) -> int
{
    int i, high, low, mid;
    int count = 0,flag = 0;
    low = 0;
    high = n - 1;
    while (high >= low)//保证右下标不小于左下标 
    {
        count++;
        mid = (high + low) / 2;
        if (key < a[mid])
            high = mid - 1;
        else if (key > a[mid])
            low = mid + 1;
        if (key-0.4 <= a[mid] && a[mid] <= key+0.4)
        {
            //元素找到了！！！\n一共查找了% d次\n它处于a[% d]位置上\na[% d] = % d\n
            //printf("元素找到了！！！\n一共查找了%d次\n它处于a[%d]位置上\na[%d]=%d\n", count, mid, mid, key);
            no = mid;
            flag = 1;
            break;
        }
    }
    if (flag == 1) {
        return mid; //返回数组下标
    }

    return -1;
}

auto MyNc4Parse::binary_select(float a[], int n, std::vector<float> keys, int& no) -> int
{
    return 0;
}

void MyNc4Parse::getAllFiles(std::string path, std::vector<std::string>& files, std::string fileType)
{
    // 文件句柄
    long long hFile = 0;
    // 文件信息
    struct _finddata_t fileinfo;
    std::string p;

    if ((hFile = _findfirst(p.assign(path).append("\\*" + fileType).c_str(), &fileinfo)) != -1) {
        do {
            // 保存文件的全路径
            files.push_back(p.assign(path).append("\\").append(fileinfo.name));

        } while (_findnext(hFile, &fileinfo) == 0);  //寻找下一个，成功返回0，否则-1

        _findclose(hFile);
    }
}
